package com.springBoot.springBootSysCore.modules.controller.web.system;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.springBoot.springBootSysCore.common.utils.returnUtil.AjaxJson;
import com.springBoot.springBootSysCore.modules.entity.system.SysDict;
import com.springBoot.springBootSysCore.modules.enums.base.DeleteStateEnum;
import com.springBoot.springBootSysCore.modules.services.system.SysDictService;
import com.springBoot.springBootSysCore.modules.controller.web.base.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统字典
 */
@Controller
@RequestMapping("${adminPath}/dict")
public class SysDictController extends BaseController<SysDict,String> {

	@Autowired
	private SysDictService sysDictService;
	/**
	 * 分页查询
	 * @param request
	 * @param model
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping("queryByPage")
	@ResponseBody
	public Object queryByPage(HttpServletRequest request,
							  Model model) throws JsonProcessingException{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("EQ_delFlag", DeleteStateEnum.NO_DELETE.getVal());
		return super.queryByPageForDataTables(model, request,map, "createDate", "desc");
	}

	@RequestMapping(value="findOne",method=RequestMethod.GET)
	public String findOne(String id,Model model){
		if(StringUtils.isNoneBlank(id)){
			model.addAttribute("entity", sysDictService.findById(id));
		}
		return "system/dict/dictChange";
	}
	/**
	 * 保存
	 * @param dict
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping(value="saveEntity",method=RequestMethod.POST)
	@ResponseBody
	public Object saveEntity(SysDict dict,Model model,HttpServletResponse response) throws JsonProcessingException{
		if (!beanValidator(model, dict)){
			return renderString(response, new AjaxJson().setMsg(model.asMap().get("message").toString()).setSuccess(false));
		}
		return sysDictService.save(dict);
	}

	@RequestMapping(value="deleteEntity",method=RequestMethod.POST)
	@ResponseBody
	public Object deleteEntity(SysDict dict) throws JsonProcessingException{
		return sysDictService.deleteEntity(dict);
	}
}
